package com.hymjweb.demo.hymj.log_manage.data_column.service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hymjweb.demo.framework.cache.bean.AMyCacheTreeNode;
import com.hymjweb.demo.framework.util.DataTypeUtil;
import com.hymjweb.demo.framework.util.pojoContrast.ContrastUtils;
import com.hymjweb.demo.framework.util.pojoContrast.FieldMeta;
import com.hymjweb.demo.hymj.log_manage.data_column.mapper.DataColumnMapper;
import com.hymjweb.demo.hymj.log_manage.service.XtBackupLogMsgService;
import org.apache.commons.beanutils.PropertyUtils;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.beans.PropertyDescriptor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * @author DaTing created on 2020-09-17
 **/
@Service
public class DataColumnService {

    @Autowired
    private DataColumnMapper dataColumnMapper;

    @Autowired
    private XtBackupLogMsgService xtBackupLogMsgService;


    public String translateColumnName(String nameColumnName, String tableName,String idColumnName,String idValue) throws Exception {
        String paramValue = "表列name：" + DataTypeUtil.toNotNullString(nameColumnName) + "；表名："+ DataTypeUtil.toNotNullString(tableName) + "；表列id：" + DataTypeUtil.toNotNullString(idColumnName)+ "；表id值：" + DataTypeUtil.toNotNullString(idValue) + "；";
        if(DataTypeUtil.isEmptyStr(nameColumnName)){
            throw new Exception(paramValue + "；翻译失败，表列name为空");
        }
        if(DataTypeUtil.isEmptyStr(tableName)){
            throw new Exception(paramValue + "；翻译失败，表名为空");
        }
        if(DataTypeUtil.isEmptyStr(idColumnName)){
            throw new Exception(paramValue + "；翻译失败，表列id为空");
        }
        if(DataTypeUtil.isEmptyStr(idValue)){
            throw new Exception(paramValue + "；翻译失败，表id值为空");
        }
        return dataColumnMapper.translateColumnName(nameColumnName, tableName,idColumnName,idValue);
    }

    public List<AMyCacheTreeNode> user_col_comments() {
        return dataColumnMapper.user_col_comments();
    }


    public Object queryPojoById(String tableName, String id,Class classObj) throws Exception{
        if(DataTypeUtil.isEmptyStr(tableName) || DataTypeUtil.isEmptyStr(id)){
            return null;
        }
        Map<String,Object> map = dataColumnMapper.getOnePojoFromDb(tableName,id);
        Object o = mapToInstance(map,classObj);
        return o;
    }

    public Object queryPojoByIds(String tableName, String ids,Class classObj) throws Exception{
        if(DataTypeUtil.isEmptyStr(tableName) || DataTypeUtil.isEmptyStr(ids)){
            return null;
        }
        List<Map<String,Object>> map = dataColumnMapper.queryByIdsFromDb(tableName,ids);
        List<Object> list = new ArrayList<>();
        for(Map<String,Object> m : map){
            Object o = mapToInstance(m,classObj);
            list.add(o);
        }
        return list;
    }

    private Object mapToInstance(Map  map,Class classObj) throws Exception{
        String jsonStr = JSONObject.toJSONString(map);//转成json对象
        JSONObject jsonObject = JSONObject.parseObject(jsonStr);
        return xtBackupLogMsgService.cloneJsonObjToInstance(jsonObject,classObj.newInstance());
    }


}
